package com.yinshan.library;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.Calendar;

/**
 * @author limh
 * @function
 * @date 2019/4/17 15:36
 */
public class CompassView extends View {
    private String TAG = "CompassView";
    private String[] shis = {"一点", "两点", "三点", "四点", "五点", "六点", "七点", "八点", "九点", "十点", "十一点", "十二点"};
    private String[] fenmiaos = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
            "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
            "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十",
            "三十一", "三十二", "三十三", "三十四", "三十五", "三十六", "三十七", "三十八", "三十九", "四十",
            "四十一", "四十二", "四十三", "四十四", "四十五", "四十六", "四十七", "四十八", "四十九", "五十",
            "五十一", "五十二", "五十三", "五十四", "五十五", "五十六", "五十七", "五十八", "五十九"};
    private Double rr = 2 * Math.PI / 60;//2π即360度的圆形f份成60份,一秒钟与一钟
    private Double rr2 = 2 * Math.PI / 12;//2π圆形份成12份,圆形显示12个小时的刻度
    //半径
    private int radius;
    //宽
    private int width;
    //高
    private int height;

    //画笔 时  分 秒
    private Paint sPaint;
    private Paint fPaint;
    private Paint mPaint;
    //中间文字
    private Paint txtPaint;
    private Rect bounds;
    private Rect clockBounds;

    private String centerText = "李";
    private float centerSize = 18f;
    private float clockTextSize = 14f;
    private int compassColor = 0xFFDDDDDD;
    private int selectColor = 0xFF000000;

    private int hour = 1;
    private int minute = 0;
    private int second = 0;

    public CompassView(Context context) {
        super(context);
        init(context, null);
    }

    public CompassView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CompassView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    /**
     * 初始化
     *
     * @param context 上下文对象
     * @param attrs   资源
     */
    private void init(Context context, @Nullable AttributeSet attrs) {
        if (null != attrs) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CompassView);
            centerText = array.getString(R.styleable.CompassView_centerText);
            centerSize = array.getDimension(R.styleable.CompassView_centerSize, 18);
            clockTextSize = array.getDimension(R.styleable.CompassView_clockTextSize, 14);
            compassColor = array.getColor(R.styleable.CompassView_compassColor, ContextCompat.getColor(context, R.color.colorTxt2));
            selectColor = array.getColor(R.styleable.CompassView_selectColor, ContextCompat.getColor(context, R.color.colorWhite));
            array.recycle();
        }
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "font_xyxs.ttf");

        bounds = new Rect();
        clockBounds = new Rect();
        txtPaint = new Paint();
        txtPaint.setColor(selectColor);
        txtPaint.setTextAlign(Paint.Align.CENTER);
        txtPaint.setTextSize(centerSize);
        txtPaint.setTypeface(typeface);

        sPaint = new Paint();
        sPaint.setColor(compassColor);
        sPaint.setTextAlign(Paint.Align.CENTER);
        sPaint.setTextSize(clockTextSize);

        fPaint = new Paint();
        fPaint.setColor(compassColor);
        fPaint.setTextAlign(Paint.Align.LEFT);
        fPaint.setTextSize(clockTextSize);

        mPaint = new Paint();
        mPaint.setColor(compassColor);
        mPaint.setTextAlign(Paint.Align.LEFT);
        mPaint.setTextSize(clockTextSize);

         animator = ValueAnimator.ofFloat(0f, 1.0f);
        animator.setDuration(1000);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                secondDegree = secondDegree - rr + animation.getAnimatedFraction() * rr;
            }
        });
    }
    ValueAnimator animator;
    private double secondDegree;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        updateTime();
        //测量文本
        txtPaint.getTextBounds(centerText, 0, centerText.length(), bounds);
        //画姓氏
        canvas.drawText(centerText, radius, radius - (float) (bounds.top + bounds.bottom) / 2, txtPaint);
        //画小时刻度
        for (int i = 0; i < shis.length; i++) {
            double degree = (i + 12 - hour + 4) * rr2;
            float x = (float) (radius + centerSize * Math.sin(degree));
            float y = (float) (radius - centerSize * Math.cos(degree));
            canvas.save();
            canvas.rotate((float) (degree / Math.PI * 180 - 90), x, y);
            sPaint.getTextBounds(shis[i], 0, shis[i].length(), clockBounds);
            if (i == hour - 1) {
                sPaint.setColor(selectColor);
            } else {
                sPaint.setColor(compassColor);
            }
            canvas.drawText(shis[i], x, y - (float) (clockBounds.top + clockBounds.bottom) / 2, sPaint);
            canvas.restore();
        }
        //画分
        for (int i = 0; i < fenmiaos.length; i++) {
            double degree = (i + 60 - minute + 15) * rr;
            float x = (float) (radius + (radius / 2 - clockTextSize) * Math.sin(degree));
            float y = (float) (radius - (radius / 2 - clockTextSize) * Math.cos(degree));
            canvas.save();
            canvas.rotate((float) (degree / Math.PI * 180 - 90), x, y);
            fPaint.getTextBounds(fenmiaos[i] + "分", 0, (fenmiaos[i] + "分").length(), clockBounds);
            if (i == minute) {
                fPaint.setColor(selectColor);
            } else {
                fPaint.setColor(compassColor);
            }
            canvas.drawText(fenmiaos[i] + "分", x, y - (float) (clockBounds.top + clockBounds.bottom) / 2, fPaint);
            canvas.restore();
        }
        //画秒
        for (int i = 0; i < fenmiaos.length; i++) {
            secondDegree = (i + 60 - second + 15) * rr;
            float x = (float) (radius + (radius - clockTextSize * 4) * Math.sin(secondDegree));
            float y = (float) (radius - (radius - clockTextSize * 4) * Math.cos(secondDegree));
            canvas.save();
            animator.start();
            canvas.rotate((float) (secondDegree / Math.PI * 180 - 90), x, y);
            mPaint.getTextBounds(fenmiaos[i] + "秒", 0, (fenmiaos[i] + "秒").length(), clockBounds);
            if (i == second) {
                mPaint.setColor(selectColor);
            } else {
                mPaint.setColor(compassColor);
            }
            canvas.drawText(fenmiaos[i] + "秒", x, y - (float) (clockBounds.top + clockBounds.bottom) / 2, mPaint);
            canvas.restore();
        }
        //每秒刷新
        postInvalidateDelayed(1000);
    }

    /**
     * 更新当前时间
     */
    private void updateTime() {
        Calendar calendar = Calendar.getInstance();
        hour = calendar.get(Calendar.HOUR);//获取小时,12小时制
        minute = calendar.get(Calendar.MINUTE);//获取分钟
        second = calendar.get(Calendar.SECOND);//获取秒
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        width = w;
        height = h;
        if (width > height) {
            radius = height / 2;
        } else {
            radius = width / 2;
        }
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int width = Math.min(widthSize, heightSize);
        if (widthMode == MeasureSpec.UNSPECIFIED) {
            width = heightSize + getPaddingRight() + getPaddingLeft();
        } else if (heightMode == MeasureSpec.UNSPECIFIED) {
            width = widthSize + getPaddingRight() + getPaddingLeft();
        }
        setMeasuredDimension(width, width);
    }
}
